package annotation;

import java.lang.annotation.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Entity {
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface ID {
    int value();
}

@Target(ElementType.PARAMETER)
@interface Validate {
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@interface Log {
}

@Log
@Entity
class User {
    @ID(100)
    private Integer id;

    public void setId(@Validate Integer id) {
        this.id = id;
    }

    @Log
    public Integer getId() {
        return id;
    }
}


public class AnnotationDemo01 {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        //1.获取类的字节码对象
        Class<User> userClass = User.class;
        //2.判定类上是否有指定注解
        boolean flag = userClass.isAnnotationPresent(Entity.class);
        System.out.println(flag);
        //3.获取类上的注解
        Entity entity = userClass.getAnnotation(Entity.class);
        System.out.println(entity instanceof Annotation);//true


        Field field = userClass.getDeclaredField("id");
        boolean hasId = field.isAnnotationPresent(ID.class);
        if (!hasId) return;
        ID id = field.getAnnotation(ID.class);
        int value = id.value();
        field.setAccessible(true);
        System.out.println(value);
        User user = new User();
        field.set(user,value);
        System.out.println(field.get(user));

        Method[] methods = userClass.getDeclaredMethods();

        int a,b,c;
        a=1;
        b=2;
        c=(a+b>3?a++:b++);
        System.out.println(a+""+b+""+c);


    }

}
